package com.hx.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Objects;
import java.util.UUID;

/**
 * 文件上传
 */
@RestController
@Slf4j
public class FileController {

    /**
     * 文件下载
     */
    @GetMapping("/download")
    public void download(String filename, HttpSession session, HttpServletResponse response) {
     try {
         String dest = session.getServletContext().getRealPath("/files/");
         //要下载的文件
         File destFile = new File(dest, filename);
         //告知浏览器以附件的形式去打开该资源
         response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
         IOUtils.copy(new FileInputStream(destFile), response.getOutputStream());
     }catch (Exception e) {
         e.printStackTrace();
     }
    }


    /**
     * 多文件上传
     * @param
     * @param session
     * @return
     */
    @PostMapping("/moreUpload")
    public String upload(@RequestParam("myFiles")MultipartFile[] myFiles, HttpSession session){

        try {
            if (Objects.isNull(myFiles) || myFiles.length == 0){
                return "文件不能为空";
            }
            for(MultipartFile myFile : myFiles) {
                String filename = myFile.getOriginalFilename();
                if (StringUtils.isEmpty(filename)){
                    continue;
                }
                log.info("filename:{}", filename);
                String substring = filename.substring(filename.lastIndexOf("."));
                //新文件名
                filename = UUID.randomUUID().toString().replaceAll("-", "").concat(substring);
                String dest = session.getServletContext().getRealPath("/files/");
                log.info("dest-----{}", dest);
                File file = new File(dest, filename);
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                IOUtils.copy(myFile.getInputStream(), new FileOutputStream(file));
            }
            return "文件上传成功";
        }catch (IOException e) {
            e.printStackTrace();
        }
        return "文件上传失败";
    }


    /**
     * 重点  @RequestParam("myFile")MultipartFile myFile
     *
     * @param myFile
     * @param session
     * @return
     */
    @PostMapping("/upload")
    public String upload(@RequestParam("myFile") MultipartFile myFile, HttpSession session) {

        try {
            String filename = myFile.getOriginalFilename();

            if (StringUtils.isEmpty(filename)) {
                return "上传文件不能为空";
            }

            log.info("filename:{}", filename);

            String substring = filename.substring(filename.lastIndexOf("."));
            //新文件名
            filename = UUID.randomUUID().toString().replaceAll("-", "").concat(substring);
            String dest = session.getServletContext().getRealPath("/files/");
            log.info("dest--->{}", dest);
            File file = new File(dest, filename);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            //文件上传
            //API一
            //IOUtils.copy(myFile.getInputStream(), new FileOutputStream(file));
            //API二
            myFile.transferTo(file);
            return "http://localhost:80/files/" + filename;

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "文件上传失败";
    }



}
